/**
 * ReportGear(2011)
 */
package com.reportgear.designer.pane.headerfooter;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import net.abbp.report.model.headerfooter.HFElement;
import net.abbp.swing.util.GuiUtils;

import com.reportgear.core.swing.pane.base.JBaseDialog;
import com.reportgear.report.model.headerfooter.editor.ElementEditor;

/**
 * 面板-页眉页脚-内部组件
 * 
 * @version 1.0 2011-4-22
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class HFComponent extends JLabel {
	/**
	 * UID
	 */
	private static final long serialVersionUID = -6159290682186908301L;
	private HFElement hfElement;
	private ActionListener moveLeftActionListener = null;
	private ActionListener moveRightActionListener = null;
	private ActionListener deleteActionListener = null;
	private ChangeListener contentChangeListener = null;
	private JPopupMenu popupMenu;

	private MouseListener editMouseListener = new MouseAdapter() {
		public void mouseReleased(MouseEvent evt) {
			if (evt.isPopupTrigger()) {
				GuiUtils.showPopupMenu(HFComponent.this.popupMenu, HFComponent.this, evt.getX(), evt.getY());
				return;
			}

			showEditor();
		}
	};

	public HFComponent(HFElement element) {
		this.setBorder(BorderFactory.createLineBorder(Color.gray));
		this.setHorizontalAlignment(JLabel.CENTER);
		this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		this.addMouseListener(this.editMouseListener);

		this.popupMenu = new JPopupMenu();
		JMenuItem mnuEdit = new JMenuItem("编辑...");
		mnuEdit.setMnemonic('E');
		this.popupMenu.add(mnuEdit);
		mnuEdit.addMouseListener(this.editMouseListener);

		this.popupMenu.addSeparator();

		JMenuItem mnuMoveLeft = new JMenuItem("左移");
		mnuMoveLeft.setMnemonic('L');
		this.popupMenu.add(mnuMoveLeft);
		mnuMoveLeft.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent paramActionEvent) {
				applyMoveLeft();
			}
		});
		JMenuItem mnuMoveRight = new JMenuItem("右移");
		mnuMoveRight.setMnemonic('R');
		this.popupMenu.add(mnuMoveRight);
		mnuMoveRight.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent paramActionEvent) {
				applyMoveRight();
			}
		});

		this.popupMenu.addSeparator();

		JMenuItem mnuDelete = new JMenuItem("删除");
		mnuDelete.setMnemonic('D');
		this.popupMenu.add(mnuDelete);
		mnuDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent paramActionEvent) {
				applyDelete();
			}
		});
		setHFElement(element);
	}

	/**
	 * 获取页眉页脚元素
	 * 
	 * @return 页眉页脚元素
	 */
	public HFElement getHFElement() {
		return this.hfElement;
	}

	/**
	 * 设置页眉页脚元素
	 * 
	 * @param element
	 *            元素
	 */
	public void setHFElement(HFElement element) {
		this.hfElement = element;
		setIcon(element.getIcon());
		setToolTipText(element.getCaption());
	}

	public ActionListener getMoveLeftActionListener() {
		return this.moveLeftActionListener;
	}

	public void setMoveLeftActionListener(ActionListener paramActionListener) {
		this.moveLeftActionListener = paramActionListener;
	}

	public ActionListener getMoveRightActionListener() {
		return this.moveRightActionListener;
	}

	public void setMoveRightActionListener(ActionListener paramActionListener) {
		this.moveRightActionListener = paramActionListener;
	}

	public ActionListener getDeleteActionListener() {
		return this.deleteActionListener;
	}

	public void setDeleteActionListener(ActionListener paramActionListener) {
		this.deleteActionListener = paramActionListener;
	}

	public ChangeListener getContentChangeListener() {
		return this.contentChangeListener;
	}

	public void setContentChangeListener(ChangeListener paramChangeListener) {
		this.contentChangeListener = paramChangeListener;
	}

	@Override
	public Dimension getPreferredSize() {
		return new Dimension(24, 24);
	}

	private void showEditor() {
		ElementEditor editor = this.hfElement.getEditor();
		if (editor == null) {
			return;
		}
		editor.populate(this.hfElement);
		if (editor.showInDialog(this) == JBaseDialog.RESULT_OK) {
			this.hfElement = editor.update();
			contentChanged();
		}
	}

	private void applyMoveLeft() {
		if (this.moveLeftActionListener == null)
			return;

		ActionEvent evt = new ActionEvent(this, 100, "Move Left");
		this.moveLeftActionListener.actionPerformed(evt);
	}

	private void applyMoveRight() {
		if (this.moveRightActionListener == null)
			return;

		ActionEvent evt = new ActionEvent(this, 100, "Move Right");
		this.moveRightActionListener.actionPerformed(evt);
	}

	private void applyDelete() {
		if (this.deleteActionListener == null)
			return;

		ActionEvent evt = new ActionEvent(this, 100, "Delete");
		this.deleteActionListener.actionPerformed(evt);
	}

	private void contentChanged() {
		if (this.contentChangeListener == null)
			return;

		ChangeEvent evt = new ChangeEvent(this);
		this.contentChangeListener.stateChanged(evt);
	}
}
